#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <vector>
#include <mpi.h>
#include "tags.h"


class Slave{
    
    public:
        Slave();	
        ~Slave();
        
        void SetParameters(int, int);
        void Start();
        
		virtual void JobHandler(char* inputBuffer, int size) { };
		
		void* WorkerThread(Slave* slave);					
		
		static void* callWorkerThread(void *arg) 
		{ 
			Slave* slav = (Slave*)arg;
			return ((Slave*)arg)->WorkerThread(slav); 
		}
		
	protected:
		int slaveId;
        int lastMessageFrom;
		
    private:  
        std::vector<pthread_t> threads;
        
        char *receiveBuffer; 
        int receiveBufferSize;
        
        void CreateThread();   
        
        void SyncThreads();     
        void CloseAllThreads();

		void ResetBufferSize();
		
		// Pierwsze 4 bajty wyslane do Slave'a to INT, jezeli przez omylke
		// ustawiono rozmiar bufora na mniej niz 4 bajty, to nalezy go 
		// zresetowac do 1024 bajtow tagiem TAG_RESET_BUFFER_SIZE
		void SetBufferSize();
		
		Slave* SlaveCopy();
};
